package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品分类列表
 *
 * */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分类列表
     * @param page
     * @param pageSize
     * @param
     * @return
     */
    @ApiOperation(value = "获取分类列表", notes = "获取分类列表")
    @RequestMapping("page")
    public Result<IPage<Category>> page(@RequestParam() Integer page, @RequestParam Integer pageSize, CategoryPageQueryDTO categoryPageQueryDTO) {
        IPage<Category> categoryIPage = new Page<>(page, pageSize);
        IPage<Category> list = categoryService.pageCategory(categoryIPage, categoryPageQueryDTO);
        return Result.success(list);
    }
    /**
     * 根据id查询分类
     * @param id
     * @return
     * */
    @ApiOperation(value = "根据id查询分类", notes = "根据id查询分类")
    @GetMapping("getById")
    public Result<Category> getById(@RequestParam Integer id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 更新分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation(value = "更新分类", notes = "更新分类")
    @PutMapping
    public Result<String> update(@RequestBody CategoryDTO categoryDTO) {
        //创建数据实体对象
        Category category = new Category();
        //复制
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.updateById(category);
        return Result.success();
    }
    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation(value = "新增分类", notes = "新增分类")
    @PostMapping
    public Result<String> save(@RequestBody CategoryDTO categoryDTO) {
        //创建数据实体对象
        Category category = new Category();
        //复制
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.save(category);
        return Result.success();
    }
    /**
     * 删除分类
     * @param id
     * @return
     */
    @ApiOperation(value = "删除分类", notes = "删除分类")
    @DeleteMapping
    public Result<String> delete(@RequestParam Integer id) {
        categoryService.removeById(id);
        return Result.success();
    }
    /**
     * 根据id修改状态
     * @param id，status
     * @return
     * */
    @ApiOperation(value = "根据id修改状态", notes = "根据id修改状态")
    @PutMapping("/status/{status}")
    public Result<String> updateStatus(@RequestParam Integer id, @RequestParam Integer status) {
        LambdaUpdateWrapper<Category> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Category::getId,id);
        wrapper.set(Category::getStatus,status);
        categoryService.update(wrapper);
        return Result.success();
    }
    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getType,type);
        List<Category> list = categoryService.list(queryWrapper);
        return Result.success(list);
    }

}
